package com.kejiguan.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kejiguan.ui.R;
import com.kejiguan.ui.model.NeItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YYY on 16/7/19.
 */
public class ModifyExhiitionNeAdapter extends RecyclerView.Adapter<ModifyExhiitionNeVH>{

    private Context context;
    private List<NeItem> neItems;
    private LayoutInflater layoutInflater;
    private View.OnClickListener onClickListener;

    public ModifyExhiitionNeAdapter(Context context,List<NeItem> neItems)
    {
        this.context=context;
        this.neItems=neItems;
        layoutInflater=LayoutInflater.from(context);
    }


    @Override
    public ModifyExhiitionNeVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=layoutInflater.inflate(R.layout.activity_modify_exhibition_ne_item,null);
        return new ModifyExhiitionNeVH(itemView);
    }

    @Override
    public void onBindViewHolder(ModifyExhiitionNeVH holder, int position) {

        NeItem neItem=neItems.get(position);

        holder.neIdView.setText(neItem.getId());
        holder.neNameView.setText(neItem.getNeName());
        holder.neIpView.setText(neItem.getNeIp());

        holder.removeBtn.setTag(R.id.recyclerview_item_position,position);
        holder.detailView.setTag(R.id.recyclerview_item_position,position);
        if (null!=onClickListener)
        {
            holder.removeBtn.setOnClickListener(onClickListener);
            holder.detailView.setOnClickListener(onClickListener);
        }
    }

    @Override
    public int getItemCount() {
        if (null==neItems)
        {
            return 0;
        }else
        {
            return neItems.size();
        }
    }

    public void setNeItem(int position,NeItem neItem)
    {

        if (null!=neItem&&position<neItems.size())
        {
            neItems.set(position,neItem);
            notifyDataSetChanged();
        }
    }
    public void addNeItem(NeItem neItem)
    {
        if (null==neItems)
        {
            neItems=new ArrayList<>(0);
        }
        neItems.add(0,neItem);
        notifyDataSetChanged();
    }

    public void removeNeItem(NeItem neItem)
    {
        if (null!=neItems)
        {
            neItems.remove(neItem);
            notifyDataSetChanged();
        }
    }

    public NeItem getNeItemByPosition(int position)
    {

        if (null!=neItems&&position<neItems.size())
        {
            return neItems.get(position);
        }else
        {
            return null;
        }
    }

    public void setNeItems(List<NeItem> neItems) {
        this.neItems = neItems;
        notifyDataSetChanged();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
}
